package MoteurJeu;

import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import GameBoard.GameBoard;
import JeuInterfaces.JeuInterfaceGraphique;
import Player.Player;

public class MoteurJeu 
{
	private GameBoard gameBoard;
	private Vector<Player> players;
	private JeuInterfaceGraphique interfaceGraphiqueJeu;
	private int IndexCurrentPlayer;
	private Player currentPlayer;
	
	public MoteurJeu(GameBoard gameBoard, Vector<Player> players, JeuInterfaceGraphique interfaceGraphiqueJeu)
	{
		this.gameBoard = gameBoard;
		this.players = players;
		this.interfaceGraphiqueJeu = interfaceGraphiqueJeu;
		gameBoard.distributePawn(players);
		this.IndexCurrentPlayer = 0;
		currentPlayer = players.get(IndexCurrentPlayer);
	}
	
	public void run()
	{
		while(true)
		{
			if(gameBoard.isFinished() && currentPlayer.isFinishedPlaying())
			{
				gameBoard.majScoreFinal();
				JOptionPane.showMessageDialog(new JFrame(), "Le Jeu est fini", "fin", JOptionPane.DEFAULT_OPTION);
				return;
			}
			currentPlayer.play(gameBoard);
			if(currentPlayer.isFinishedPlaying())
			{	
				updateCurrentPlayer();
				gameBoard.majScoreTour();
			}
			
			interfaceGraphiqueJeu.update();
			if(!currentPlayer.isAutonome())
				return;
		}
	}
	
	public void updateCurrentPlayer()
	{
		IndexCurrentPlayer = (IndexCurrentPlayer+1)%players.size();
		currentPlayer = players.get(IndexCurrentPlayer);
	}	
	
	
	//getters
	public GameBoard getGameBoard(){
		return gameBoard;
	}
	public Vector<Player> getPlayers(){
		return players;
	}
	public Player getCurrentPlayer(){
		return currentPlayer;
	}
	//setters
	public void setGameBoard(GameBoard gameBoard){
		this.gameBoard = gameBoard;
	}
	public void setPlayers(Vector<Player> players){
		this.players = players;
	}
}
